package com.define.ssh.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


 







import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.define.ssh.dao.DeptAccountDao;
import com.define.ssh.model.DeptAccount;

@Repository("deptAccountDaoImpl")
public class DeptAccountDaoImpl extends BaseDaoImpl<DeptAccount, Long>
		implements DeptAccountDao {

	/***
	 * 删除该用户 对应的部门
	 * 
	 * @param user_id
	 */
	@Override
	public void delDeptAccountByUserId(Long user_id) {
		String sql = "delete from deptaccount where userid=" + user_id;
		getSession().createSQLQuery(sql).executeUpdate();
		System.out.println("已经删除");
	}

	@Override
	public void save(List<Object[]> list) {
		if (list != null && list.size() > 0)
		{
			SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
			String start=sdf.format(new Date());
			
			//批量导入start
		    Session session = this.sessionFactory.openSession();//  HibernateUtil.getSessionFactory().getCurrentSession();   
		    
		    Transaction tran = session.beginTransaction(); 
	           session.setCacheMode(CacheMode.IGNORE);   
	           PreparedStatement stmt;   
	           try {
	               stmt = session.connection().prepareStatement("insert into deptaccount(deptid,userid) " +
	               		"values(?,?)");   
	               for(int i=0;i<list.size();i++){
	            	   Object[] obj=list.get(i);
	            	   stmt.setInt(1, (Integer) obj[0]);
	            	   stmt.setInt(2, (Integer) obj[1]);
	            	   stmt.addBatch();
	            	   stmt.clearParameters();
	            	   if((i%200==0||list.size()-1==i)&&i>0){
	            		   stmt.executeBatch();
	            		   stmt.clearBatch();
	            		   
	            	   }
	               }
	
	           } catch (SQLException e) {   
	               e.printStackTrace();    
	               tran.rollback();   
	           }
	           System.out.println("------------");
	           tran.commit();   
	           System.out.println("=============");
	           session.close();
	            
			String end=sdf.format(new Date());
			System.out.println("导入完成，导入条数："+list.size());
			System.out.println("开始时间："+start);
			System.out.println("结束时间："+end);
		}
	}
}
